<template>
  <el-dialog :close-on-click-modal='false' v-drag :title="record.id?$t('编辑') : $t('添加')" :v-if="dialogVisible" :visible.sync="dialogVisible" width="520px" top='10vh' @close='close'>
    <div class="dialogContentBoxMaxHeight80">
      <el-form ref="form" :model="record" label-width="130px" :rules="rules" inline label-position="left" style="margin:0 30px;">
        <el-form-item :label="$t('所属接口')" prop="interface_name">
          <el-select v-model="record.interface_name" ref="interface" filterable size='small' :disabled='record.id !== null' style="width:267px;">
            <el-option v-for="item in policyList" :key="item.name" :label="item.name" :value="item.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('出方向带宽通道名称')" prop="name">
          <el-input v-emoji v-model="record.name" ref="name" style="width:267px;" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('优先级')" prop="priority">
          <el-input v-emoji v-model="record.priority" ref="priority" style="width:267px;" size='small'></el-input>
        </el-form-item>
        <el-form-item :label="$t('保证带宽')" prop="gurantee">
          <el-input v-emoji v-model="record.gurantee" ref="gurantee" style="width:267px;" size='small'>
            <el-select slot="append" v-model="record.gurantee_unit" ref="gurantees" filterable size='small' style="width:100px;">
              <el-option v-for="item in guaranteeLimitList" :key="item" :label="item" :value="item"></el-option>
            </el-select>
          </el-input>
        </el-form-item>
        <el-form-item :label="$t('限制带宽')" prop="limit">
          <el-input v-emoji v-model="record.limit" ref="limit" style="width:267px;" size='small'>
            <el-select slot="append" v-model="record.limit_unit" ref="limitUnit" filterable size='small' style="width:100px;">
              <el-option v-for="item in guaranteeLimitList" :key="item" :label="item" :value="item"></el-option>
            </el-select>
          </el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submit" class="addSubmit">{{$t('确 定')}}</el-button>
      <el-button @click="close" class="addClose">{{$t('取 消')}}</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getBandWidthArray, addBandWidth, editBandWidth } from '@/api/network/bandwidth'
import { inputClass, selectClass } from '@/utils/input-class.js'
export default {
  name: 'ClassifyDialog',
  data () {
    var validateName = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else {
        callback()
      }
    }
    var validatePriority = (rule, value, callback) => {
      if (String(value).trim() === '') {
        callback(new Error(this.$t('不能为空')))
      } else if (Number(value) < 0 || Number(value) > 7) {
        callback(new Error(this.$t('优先级不合法,优先级取值范围为：0-7')))
      } else {
        callback()
      }
    }
    var validateLimit = (rule, value, callback) => {
      let gurantee = this.record.gurantee * this.guaranteeLimitNum[this.record.gurantee_unit]
      let limit = this.record.limit * this.guaranteeLimitNum[this.record.limit_unit]
      if (value && Number(limit) < Number(gurantee)) {
        callback(new Error(this.$t('限制带宽不为空时,不能小于保证带宽!')))
      } else {
        callback()
      }
    }
    return {
      dialogVisible: false,
      record: {
        id: null,
        interface_name: '',
        dad_name: '',
        name: '',
        priority: '',
        gurantee: '',
        gurantee_unit: 'Kbps',
        limit: '',
        limit_unit: 'Kbps',
        old_name: ''
      },
      recordCopy: {
        id: null,
        interface_name: '',
        dad_name: '',
        name: '',
        priority: '',
        gurantee: '',
        gurantee_unit: 'Kbps',
        limit: '',
        limit_unit: 'Kbps',
        old_name: ''
      },
      policyList: [],
      guaranteeLimitList: ['bps', 'Kbps', 'Mbps'],
      guaranteeLimitNum: {
        'bps': 1,
        'Kbps': 1000,
        'Mbps': 1000000
      },
      rules: {
        interface_name: [{ required: false, validator: validateName, trigger: 'blur' }],
        name: [{ required: false, validator: validateName, trigger: 'blur' }],
        priority: [{ required: false, validator: validatePriority, trigger: 'blur' }],
        gurantee: [{ required: false, validator: validateName, trigger: 'blur' }],
        limit: [{ required: false, validator: validateLimit, trigger: 'blur' }]
      }
    }
  },
  methods: {
    init (data) {
      this.dialogVisible = true
      this.$nextTick(() => {
        const data = ['priority', 'gurantee', 'limit', 'name']
        const selectData = ['interface', 'gurantees', 'limitUnit']
        inputClass(this.$refs, data)
        selectClass(this.$refs, selectData)
      })
      if (data) {
        Object.keys(this.record).forEach((key) => {
          this.record[key] = data[key]
        })
        this.record.old_name = this.record.name
      } else {
        getBandWidthArray().then(res => {
          if (res.status === 0) {
            this.policyList = res.info
            if (!data && this.policyList && this.policyList.length > 0) {
              this.record.interface_name = this.policyList[0].name
            }
          }
        })
      }
    },
    submit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.record.id) {
            editBandWidth(this.record).then(res => {
              if (res.status === 0) {
                this.$message({
                  message: this.$t('编辑成功'),
                  type: 'success'
                })
                this.close()
                this.$parent.listData()
              }
            })
          } else {
            addBandWidth(this.record).then(res => {
              if (res.status === 0) {
                this.$message({
                  message: this.$t('添加成功'),
                  type: 'success'
                })
                this.close()
                this.$parent.listData()
              }
            })
          }
        }
      })
    },
    close () {
      this.dialogVisible = false
      this.record = Object.assign({}, this.recordCopy)
      this.policyList = []
      this.$refs.form.resetFields()
    }
  }
}
</script>
<style lang="less">
.el-input-group__append {
  background-color: #ffffff;
}
</style>
<style>
body {
  margin: 0;
}
</style>
